package com.fiximusic.common;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.UUID;

public class Util {

	/**
	 * 得到UUID
	 * @return
	 */
	public static String generateUUID(){
		UUID gUUid =UUID.randomUUID();
		String uudi = gUUid.toString();
		return uudi.replaceAll("-", "");
	}
	
	/**
	 * 控制小数位数
	 */
	public static double getDecimal(double ori_decimal,int digit){
		StringBuffer string = new StringBuffer();
		string.append("#.");
		for(int  i = 0;i < digit; i++){
			string.append(0);
		}
		DecimalFormat df = new DecimalFormat(string.toString());
		return Double.parseDouble(df.format(ori_decimal));
	}
	
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };

	/**
	 * md5加密
	 * 
	 * @throws NoSuchAlgorithmException
	 */
	public static String makeMD5(String password){
		if(password == null || password.isEmpty()){
			return null;
		}
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			return byteToString(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	// 返回形式为数字跟字符串
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		// System.out.println("iRet="+iRet);
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	/**
	 * 删除文件
	 */
	public static boolean delFile(String fileUrl) {
		File tempFile = new File(fileUrl);
		if(tempFile.exists()) {
			tempFile.deleteOnExit();
			return tempFile.delete();
		}
		return false;
	}
	
	/**
	 * 文件是否存在
	 */
	public static boolean isExists(String fileUrl) {
		return new File(fileUrl).exists();
	}
	
	/**
	 * 文件是否存在
	 */
	public static boolean isExists(File file) {
		return file.exists();
	}
	
	/**
	 * 判断字符串是否为空或者为"",为空则返回true
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		return s == null || s.trim().isEmpty();
	}
	
	/**
	 * 判断list是否为空或者为"",为空则返回true
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(List<?> lists) {
		return lists == null || lists.isEmpty();
	}
	
	/**
	 * 判断数组是否为null或者空数组
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(int[] s) {
		return s == null || s.length == 0;
	}
	
	/**
	 * 判断数组是否为null或者空数组
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(double[] s) {
		return s == null || s.length == 0;
	}
	
	/**
	 * 判断数组是否为null或者空数组
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String[] s) {
		return s == null || s.length == 0;
	}
	
	/**
	 * 将字符串以符号分隔成数组
	 */
	public static String[] split2String(String str, String s) {
		if(isEmpty(str)) {
			return null;
		}
		return str.split(s);
	}
	
	/**
	 * 将字符数组转化为int数组
	 */
	public static int[] split2Double(String str, String s) {
		if(isEmpty(str)) {
			return null;
		}
		String st[] = str.split(s);
		int in[] = new int[st.length];
		for (int i = 0, j = st.length; i < j; ++i) {
			in[i] = Integer.valueOf(st[i]);
		}
		return in;
	}
	
	/**
	 * 判断数组里面是否有有个值
	 */
	public static boolean contains( String s[], String ss) {
		if(s == null || s.length == 0) {
			return false;
		}
		for (int i = 0, j = s.length; i < j; i++) {
			if(s[i].equals(ss.trim())) {
				return true;
			}
		}
		return false;
	}
	/**
	 * @param args
	 * @throws NoSuchAlgorithmException
	 */
	public static void main(String[] args) throws NoSuchAlgorithmException {
//		System.out.println(Util.makeMD5("admin"));
//		String s = null;
//		System.out.println(s.isEmpty());
//		System.out.println(split2Double("1,0,0,0,0,0,0,0,0,0", ",")[0]);
//		System.out.println("asdf.sd".substring( 0, "asdf.sd".indexOf(".")));
//		double a = Util.getDecimal(9.3214343543543, 3);
//		System.out.println(a);
//		StringBuffer s = new StringBuffer( 128).append("A").append("&").append("B").append("&");
//		System.out.println(s.substring( 0, s.lastIndexOf("&")));
//		StringBuffer s = new StringBuffer(216).append("sssssss").append("aaaaaa");
//		System.out.println(s);
//		s.setLength( 0);
//		System.out.println(s);
//		s.append("ddddddddd,");
//		System.out.println(s.deleteCharAt(s.length() - 1));
//		HashMap<String, Integer> map = new HashMap<>();
//		String s;
//		for (int i = 0; i < 10; i++) {
//			s = "s";
//			map.put( s, 1);
//		}
//		String s = "s";
//		String d = "";
//		System.out.println( !(Util.isEmpty(s) || Util.isEmpty(d)));
		String date = " 2013-02-23";
		date.trim();
	}
	
	
}
